<script setup>
import {
    Management,
    Promotion,
    UserFilled,
    User,
    Crop,
    EditPen,
    House,
    Delete,
    SwitchButton,
    CaretBottom,
    ChatRound,
    Message
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'

//导入接口函数
import { userInfoService } from '@/api/user.js'
//导入pinia
import useUserInfoStore from '@/stores/userInfo';
const userInfoStore = useUserInfoStore();
import { ref } from 'vue'

//获取个人信息
const getUserInfo = async () => {
    let result = await userInfoService(-1);
    //存储pinia
    userInfoStore.info = result.data;
}
getUserInfo()

//条目被点击
import { useRouter } from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus';
import { useTokenStore } from '@/stores/token';
const tokenStore = useTokenStore();
const router = useRouter();
const handleCommand = (command) => {
    if (command === 'logout') {
        //退出登录
        ElMessageBox.confirm(
            '是否确定退出登录',
            '温馨提示',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
            }
        )
            .then(async () => {
                //清空
                tokenStore.removeToken()
                userInfoStore.removeInfo()
                router.push('/login')
                ElMessage({
                    type: 'success',
                    message: '退出登录成功',
                })
            })
            .catch(() => {
                console.log("取消退出")
                ElMessage({
                    type: 'info',
                    message: '取消退出登录',
                })
            })
    } else {
        //路由
        if (command === 'info') {
            router.push('/user/info')
        } else if (command === 'avatar') {
            router.push('/user/avatar')
        } else if (command === 'resetPassword') {
            router.push('/user/resetPassword')
        }

    }
}

</script>

<template>
    <el-container class="layout-container">
        <!-- 头部区域 -->
        <el-header>
            <el-row :gutter="0">
                <el-col :span="22">
                    <el-menu active-text-color="#ffd04b" mode="horizontal" background-color="#232323" text-color="#fff"
                        router>
                        <el-menu-item index="/activities/manage">
                            <el-icon>
                                <Promotion />
                            </el-icon>
                            <span>搭子汇聚</span>
                        </el-menu-item>
                        <el-menu-item index="/user/manage" v-show="userInfoStore.info.isAdmin === 1">
                            <el-icon>
                                <ChatRound />
                            </el-icon>
                            <span>搭友们</span>
                        </el-menu-item>
                        <el-menu-item index="/activities/all">
                            <el-icon>
                                <Management />
                            </el-icon>
                            <span>已加入搭子</span>
                        </el-menu-item>
                        <el-menu-item index="/activities/add">
                            <el-icon>
                                <House />
                            </el-icon>
                            <span>发布搭子</span>
                        </el-menu-item>
                        <el-menu-item index="/news/newsList">
                            <el-icon>
                                <Message />
                            </el-icon>
                            <span>公告</span>
                        </el-menu-item>
                        <el-sub-menu>
                            <template #title>
                                <el-icon>
                                    <UserFilled />
                                </el-icon>
                                <span>个人中心</span>
                            </template>
                            <el-menu-item index="/user/info">
                                <el-icon>
                                    <User />
                                </el-icon>
                                <span>基本资料</span>
                            </el-menu-item>
                            <el-menu-item index="/user/avatar">
                                <el-icon>
                                    <Crop />
                                </el-icon>
                                <span>更换头像</span>
                            </el-menu-item>
                            <el-menu-item index="/user/resetPassword">
                                <el-icon>
                                    <EditPen />
                                </el-icon>
                                <span>重置密码</span>
                            </el-menu-item>
                            <el-menu-item index="/user">
                                <el-icon>
                                    <Delete />
                                </el-icon>
                                <span>注销账号</span>
                            </el-menu-item>
                        </el-sub-menu>

                    </el-menu>
                </el-col>
                <el-col :span="2" style="background-color: #232323;">
                    <!-- 右上角 -->
                    <el-dropdown @command="handleCommand" style="float: right; margin-top: 8px;">
                        <span class="el-dropdown__box">
                            <el-avatar :src="userInfoStore.info.photo ? userInfoStore.info.photo : avatar" />
                            <el-icon>
                                <CaretBottom />
                            </el-icon>
                        </span>
                        <template #dropdown>
                            <el-dropdown-menu>
                                <el-dropdown-item command="avatar" :icon="Crop">更换头像</el-dropdown-item>
                                <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
                            </el-dropdown-menu>
                        </template>
                    </el-dropdown>

                </el-col>
            </el-row>
        </el-header>
        <!-- 中间区域 -->
        <el-main>
            <!-- <div style="width: 1290px; height: 570px;border: 1px solid red;">
                内容展示区
            </div> -->
            <router-view></router-view>
        </el-main>
        <!-- 底部区域 -->
        <el-footer>
            <div>
                <p v-if="userInfoStore.info.isAdmin === 1">管理员:&nbsp;<strong>{{ userInfoStore.info.username
                        }}</strong></p>
                <p v-else>用户:&nbsp;<strong>{{ userInfoStore.info.username }}</strong></p>
            </div>
            &nbsp; © 群英聚汇
        </el-footer>
    </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
    height: 100vh;

    .el-header {

        background-color: #fff;
        background-color: #232323;
        align-items: center;
        justify-content: space-between;

        .el-dropdown__box {
            display: flex;
            align-items: center;

            .el-icon {
                color: #999;
                margin-left: 10px;
            }

            &:active,
            &:focus {
                outline: none;
            }
        }

    }

    .el-main {

        background: url('@/assets/background.jpg') no-repeat 0px 0px;
        background-size: cover;
        background-repeat: no-repeat;
        background-position: center center;
        opacity: 0.9;
    }

    .el-footer {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        color: #666;
    }
}
</style>